<html>
<head>

<title>Groovy Goodness: Using the ServletCategory</title>

<script language="javascript" src="scripts/shCore.js"></script> 
<script language="javascript" src="scripts/shLegacy.js"></script> 
<script language="javascript" src="scripts/shBrushJava.js"></script> 
<script language="javascript" src="scripts/shBrushXml.js"></script> 
<script language="javascript" src="scripts/shBrushJScript.js"></script> 
<script language="javascript" src="scripts/shBrushGroovy.js"></script> 
<script language="javascript" src="scripts/shBrushPlain.js"></script> 
<script language="javascript" src="scripts/shBrushBash.js"></script> 
 
<link href="styles/reset.css" rel="stylesheet" type="text/css" />
<link href="styles/shCore.css" rel="stylesheet" type="text/css" />
<link type="text/css" rel="stylesheet" href="styles/shThemeRDark.css"/>
<link href="styles/blog.css" rel="stylesheet" type="text/css" />

</head>
<body>

<a href="index.html">Back to index</a>

<h3 class="post-title">Groovy Goodness: Using the ServletCategory</h3>

<div class="post">
<p>In the <code>groovy.servlet</code> package we can find the class <code>ServletCategory</code>. We can use this class to access attributes on the servlet context, page context, request and session with the dot notation. The methods <code>putAt</code> and <code>getAt</code> are implemented in this class for all four objects. If we write Groovy code in the context of these object we can use the <code>ServletCategory</code> to make the code more Groovy.</p>
<p>In the following sample we write two servlets that use the <code>ServletCategory</code> to write and read attribute values. We compile the code to Java classes to get executable servlet. Finally we write a Groovy script to run Jetty with our servlets.</p>
<pre class="brush:groovy">
// File: Start.groovy
import javax.servlet.http.*
import javax.servlet.*
import groovy.servlet.ServletCategory

class Start extends HttpServlet {
    def application 
    
    void init(ServletConfig config) {
        super.init(config)
        application = config.servletContext
        use(ServletCategory) {
            application.author = 'mrhaki'
        }
    }
    
    void doGet(HttpServletRequest request, HttpServletRespons response) {
        def session = request.session
        use (ServletCategory) {
            if (session.counter) {  // We can use . notation to access session attribute.
                session.counter++  // We can use . notation to set value for session attribute.
            } else {
                session.counter = 1
            }
            
            request.pageTitle = 'Groovy Rocks!'
        }
        application.getRequestDispatcher('/output').forward(request, response)
    }
}
</pre>
<pre class="brush:groovy">
// File: Output.groovy
import javax.servlet.http.*
import javax.servlet.*
import groovy.xml.*
import groovy.servlet.ServletCategory

class Output extends HttpServlet {
    def context 
    
    void init(ServletConfig config) {
        super.init(config)
        context = config.servletContext
    }
    
    void doGet(HttpServletRequest request, HttpServletRespons reponse) {
        def html = new MarkupBuilder(response.writer)
        def session = request.session
        
        use(ServletCategory) {
            html.html {
                head {
                    title request.pageTitle
                }
                body {
                    h1 request.pageTitle 
                    h2 "$context.version written by $context.author"
                    p "You have requested this page $session.counter times."
                }
            }
        }
        
    }
}
</pre>
<pre class="brush:groovy">
// File: run.groovy
import org.mortbay.jetty.*
import org.mortbay.jetty.servlet.*
import groovy.servlet.*

@Grab(group='org.mortbay.jetty', module='jetty-embedded', version='6.1.14')
def startJetty() {
    def jetty = new Server(9090)
    def context = new Context(jetty, '/', Context.SESSIONS)
    context.resourceBase = '.'
    context.addServlet Start, '/start'
    context.addServlet Output, '/output'
    context.setAttribute 'version', '1.0'
    jetty.start()
}

startJetty()
</pre>
<p>In our web browser we open http://localhost:9090/start and get the following output:</p>
<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_-vJw6r2W-bw/SxWK_kdeZDI/AAAAAAAADKc/rcGij0DlXvo/s1600/blog-gg-servletcategory.png"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 320px; height: 250px;" src="http://4.bp.blogspot.com/_-vJw6r2W-bw/SxWK_kdeZDI/AAAAAAAADKc/rcGij0DlXvo/s320/blog-gg-servletcategory.png" border="0" alt=""id="BLOGGER_PHOTO_ID_5410383352079213618" /></a
</div>

<script language="javascript"> 
SyntaxHighlighter.config.bloggerMode = true;
SyntaxHighlighter.config.clipboardSwf = 'scripts/clipboard.swf';
SyntaxHighlighter.defaults['first-line'] = 0;
SyntaxHighlighter.defaults['auto-links'] = false;
SyntaxHighlighter.all();
dp.SyntaxHighlighter.HighlightAll('code');
</script>

</body>
</html>